package src.E_Thread;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.net.URL;

// 练习Thread，实现多线程同步下载图片
public class E01_CreateThread_ThradTest extends Thread{
    private String url;
    private String name;
    public E01_CreateThread_ThradTest(String url, String name){
        this.url = url;
        this.name = name;
    }

    @Override
    public void run() {
        new WebDownloader().downloader(url, name);
        System.out.println(name+" 下载成功！");
    }

    public static void main(String[] args) {
        E01_CreateThread_ThradTest t1 = new E01_CreateThread_ThradTest("https://img-blog.csdn.net/20150309140927553","src\\E_Thread\\1.jpg");
        E01_CreateThread_ThradTest t2 = new E01_CreateThread_ThradTest("https://img-blog.csdnimg.cn/20210512174955123.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ0NzE1OTQz,size_16,color_FFFFFF,t_70#pic_center","src\\E_Thread\\2.jpg");
        E01_CreateThread_ThradTest t3 = new E01_CreateThread_ThradTest("https://img-blog.csdnimg.cn/20210512175007490.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ0NzE1OTQz,size_16,color_FFFFFF,t_70#pic_center","src\\E_Thread\\3.jpg");
        t1.start();
        t2.start();
        t3.start();
    }
}

// 下载器
class WebDownloader{
    // 下载方法
    public void downloader(String url, String name){
        try {
            FileUtils.copyURLToFile(new URL(url), new File(name));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}